package com.gcsoftware.utils.elasticsearch;

import com.gcsoftware.web.beans.returnPage.DateSection;

import java.util.ArrayList;
import java.util.List;

import static com.gcsoftware.configs.AppConstants.Elasticsearch.*;
import static com.gcsoftware.configs.AppConstants.Sys.*;

/**
 * 字符串工具类
 * @author czp
 * date : 2018-04-10
 */
public class StringUtils {
    /**
     * 将字符串拆分
     * @param string 字符串
     * @param splitTag 拆分标记
     * @return Set
     */
    public static List<String> StringSplit(String string, String splitTag) {
        if (string == null || !org.apache.commons.lang3.StringUtils.isNotBlank(string)) return null;
        String[] strBuf = string.split(splitTag);
        List<String> rStrBuf = new ArrayList<String>();
        for (String str : strBuf) {
            rStrBuf.add(str.trim());
        }
        return rStrBuf;
    }

    /**
     * 获取分页时间范围查询语句
     * @param range 时间段
     * @return 语句
     */
    public static String queryPageRange(String range, String measure) {
        if (range == null || !org.apache.commons.lang3.StringUtils.isNotBlank(range)) return "{}";
        StringBuffer rRange = new StringBuffer();
        DateSection dateSection = strTolong(range, measure);
        long startTime = dateSection.getStartTime();
        long endTime = dateSection.getEndTime();
        rRange.append("{\"range\":{\"")
              .append(ES_CREATE_TIME.getValue())
              .append("\":{")
              .append("\"gte\":").append(startTime).append(",")
              .append("\"lte\":").append(endTime + 288000000)
              .append("}}}");
        return rRange.toString();
    }

    /**
     * 获取分组时间范围查询语句
     * @param range 时间段
     * @param measure 度量
     * @return 语句
     */
    public static String queryGroupRange(String range, String item, String measure) {
        if (range == null || !org.apache.commons.lang3.StringUtils.isNotBlank(range)) return "{}";
        StringBuffer query = new StringBuffer();
        DateSection dateSection = strTolong(range, measure);
        long startTime = dateSection.getStartTime();
        long endTime = dateSection.getEndTime();
        query.append("{\"bool\":{\"filter\":[{\"range\":{\"")
                .append(ES_CREATE_TIME.getValue())
                .append("\":{\"gte\":").append(startTime)
                .append(",\"lte\":").append(endTime + 288000000)
                .append("}}}]");
        if (item != null && !"".equals(item)) {
            query.append(",\"must\":[{\"")
                 .append(ES_MATCH_PHRASE_TAG.getValue())
                 .append("\":{\"")
                 .append(ES_SENSITIVE_ITEM_NAMES.getValue())
                 .append("\":\"")
                 .append(item)
                 .append("\"}}]");
        }
        query.append("}}");
        return query.toString();
    }

    public static String queryTerms(String apiName, String appName) {
        StringBuffer rTerms = new StringBuffer();
        if (apiName != null && org.apache.commons.lang3.StringUtils.isNotBlank(apiName)){
            rTerms.append("{\"term\":{\"")
                  .append(ES_API_NAME.getValue())
                  .append(".keyword\":\"")
                  .append(apiName)
                  .append("\"}}");
        } else {
            rTerms.append("{}");
        }
        if (appName != null && org.apache.commons.lang3.StringUtils.isNotBlank(appName)){
            rTerms.append(",{\"term\":{\"")
                    .append(ES_APP_NAME.getValue())
                    .append(".keyword\":\"")
                    .append(appName)
                    .append("\"}}");
        } else {
            rTerms.append(",{}");
        }
        return rTerms.toString();
    }

    private static DateSection strTolong(String range, String measure) {
        long startTime = 0;
        long endTime = 0;
        List<String> time = StringUtils.StringSplit(range, SYS_STR_SPLIT.getValue());
        if (measure.equals(SYS_MEASURE_SECOND.getValue())) {
            startTime = TimeUtils.strTimeToLong(time.get(0), measure);
            endTime = TimeUtils.strTimeToLong(time.get(1), measure);
        } else if (measure.equals(SYS_MEASURE_DAY.getValue())) {
            startTime = TimeUtils.strTimeToLong(time.get(0), measure);
            endTime = TimeUtils.strTimeToLong(time.get(1), measure);
        }
        return new DateSection(startTime, endTime);
    }
}
